package ServerUDP;

import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;

public class ThroughputUDP extends Thread {
    DatagramChannel channel;
    Selector selector;
    double size;
    ByteBuffer writebuffer,readBuffer;
    int flag;//用来记录是不是已经发过了
    private static Charset charset=Charset.forName("utf8");
    public ThroughputUDP(DatagramChannel c,Selector s){
        size = 0.0;
        flag=0;
        channel=c;
        selector=s;
        writebuffer=ByteBuffer.allocate(1024);//写缓冲
        readBuffer = ByteBuffer.allocate(1024);//读缓冲
    }

    @Override
    public void run(){
        try {
            while (true){
            //没有事件发生
                if(selector.select(1000)<1){
                    break;
                }
                //获取发生的事件
                Set<SelectionKey> keys=selector.selectedKeys();
                Iterator<SelectionKey> it=keys.iterator();//用于检测事件发生
                while(it.hasNext()){
                    //获取事件，移除当前事件
                    SelectionKey key=it.next();
                    it.remove();
                    if(key.isReadable()){
                        DatagramChannel datagramChannel=(DatagramChannel) key.channel();
                        readBuffer.clear();
                        SocketAddress sa= null;
                        sa = datagramChannel.receive(readBuffer);
                        readBuffer.flip();
                        //如果接收到的是kaishi，说明要测试吞吐量，需要发送较大数量的包
                        if(charset.decode(readBuffer).toString().equals("kaishi")) {
                            if(flag==0){
                                long time1=System.currentTimeMillis();
                                long time2=time1;
                                byte[] b = new byte[1024];
                                Arrays.fill(b, (byte) 1);

                                System.out.println("子线程建立开始发包！");
                                int havesent=0;
                                while (time2-time1<1000) {//发包1秒
                                    writebuffer.put(b);
                                    writebuffer.flip();
                                    channel.send(writebuffer, sa);
                                    writebuffer.clear();
                                    ++havesent;
                                    if(havesent==2){
                                        Thread.sleep(1);
                                        havesent=0;
                                    }
                                    size += 0.001;//每次传输1024B=1Kb=0.001Mb
                                    time2 = System.currentTimeMillis();
                                }
                                System.out.println(size);
                                flag=1;//标记已经发过了
                            }
                            writebuffer.put((size+"").getBytes());
                            writebuffer.flip();
                            channel.send(writebuffer, sa);
                            writebuffer.clear();
                        }
                    }
                }
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

